package com.example.datepicker.slice;

import com.example.datepicker.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.DatePicker;
import ohos.agp.components.Text;

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        // 获取DatePicker实例
        DatePicker datePicker = (DatePicker) findComponentById(ResourceTable.Id_date_pick);
        int day = datePicker.getDayOfMonth();
        int month = datePicker.getMonth();
        int year = datePicker.getYear();
//        datePicker.updateDate(2021, 8, 8);// 设置当前日期
        Text selectedDate = (Text) findComponentById(ResourceTable.Id_text_date);
        datePicker.setValueChangedListener(
            new DatePicker.ValueChangedListener() {
                @Override
                public void onValueChanged(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
                    selectedDate.setText("选择的日期："+String.format("%02d/%02d/%4d", dayOfMonth, monthOfYear, year));
                }
            }
        );
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
